package com.boxtelecom.users;

import java.util.ArrayList;
import java.util.Iterator;

import com.boxtelecom.signe.ListAstro;


import android.graphics.Bitmap;
import android.util.Log;

public class ListFriend {
	
	private ArrayList<FacebookUser> _friendList;
	private int _size;
	
	public ListFriend(){
		_friendList = new ArrayList<FacebookUser>();
		_size = 0;
	}
	
	public void add(FacebookUser aFriend){
		_friendList.add(aFriend);
	}
	
	public FacebookUser getUserFromId(String id){
		Iterator<FacebookUser> it = getIterator();
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				return tmp;
			}
		}
		
		return null;
	}
	
	public int getSize(){
		return _size;
	}
	
	public void setSize(int aSize){
		this._size = aSize;
	}
	
		
	public Iterator<FacebookUser> getIterator(){
		Iterator<FacebookUser> it = _friendList.iterator();
		return it;
	}
	
	public void setFirstNameFromId(String anId, String aName){
		Iterator<FacebookUser> it =_friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				tmp.setFirstName(aName);
			}
		}
	}
	
	public void setLastNameFromId(String anId, String aName){
		Iterator<FacebookUser> it =_friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				tmp.setLastName(aName);
			}
		}
	}
	
	public void setBirthdayFromId(String anId, String aName){
		Iterator<FacebookUser> it =_friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				tmp.setBirthday(aName);
			}
		}
	}

	public String getFirstNameFromId(String anId) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				return tmp.getFirstName();
			}
		}
		
		return null;
	}

	public String getLastNameFromId(String anId) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				return tmp.getLastName();
			}
		}
		
		return null;
	}

	public FacebookUser get(int i) {
		// TODO Auto-generated method stub
		return _friendList.get(i);
	}
	
	public void deleteListFriend(){
		Iterator<FacebookUser> it = _friendList.iterator();
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			tmp.deleteUser();
		}
	}
	
	public ArrayList<FacebookUser> getArrayList(){
		return _friendList;
	}

	public void setSigneAstroFromIdAndBirthday(String anId, String aBirthday) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it =_friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				tmp.setSigneAstroFromBirthday(aBirthday);
			}
		}
	}

	public String getSigneAstroFromId(String anId) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(anId)){
				return tmp.getSigneAstro();
			}
		}
		return null;
	}

	public void setSigneAstroIdFromId(String id, int anId) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
				
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				tmp.setIdSigneAstro(anId);
			}
		}
	}

	public void setSigneAstroFromId(String id, String string) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				tmp.setSigneAstro(string);
			}
		}
		
	}

	public String getBirthdayFromId(String id) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				return tmp.getBirth();
			}
		}
		
		return null;
	}

	public int getIdSigneAstromFromId(String id) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				return tmp.getIdSigneAstro();
			}
		}
		return 0;
	}
	
	public Presence getPresenceFromId(String id) {
		// TODO Auto-generated method stub
		for(FacebookUser friend: getArrayList()){
			if(friend.getId().equals(id)){
				return friend.getPresence();
			}
		}
		return null;
	}

	public void setJidFromId(String id, String fjid) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				tmp.setFjid(fjid);
			}
		}
	}
	
	public void setPresenceFromId(String id, Presence aPresence) {
		// TODO Auto-generated method stub
		Iterator<FacebookUser> it = _friendList.iterator();
		
		while(it.hasNext()){
			FacebookUser tmp = it.next();
			if(tmp.getId().equals(id)){
				tmp.setPresence(aPresence.getType());
			}
		}
	}

	public void setPresenceColorFromId(String id, int aColor) {
		// TODO Auto-generated method stub
		for(FacebookUser friend: getArrayList()){
			if(friend.getId().equals(id)){
				friend.setColorPresence(aColor);
			}
		}
	}
	
	public void setPictureFromId(String id, Bitmap bitmap) {
		// TODO Auto-generated method stub
		
		for(FacebookUser tmp : _friendList){
			if(tmp.getId().equals(id)){
				tmp.setPicture(bitmap);
			}		
		}
	}
	
	public void setPicturePathFromid(String id, String picturePath) {
		// TODO Auto-generated method stub
		for(FacebookUser tmp : _friendList){
			if(tmp.getId().equals(id)){
				tmp.setPicturePath(picturePath);
			}
		}
	}
	
	public void setNameColorFromId(String id, int color) {
		// TODO Auto-generated method stub
		
		for(FacebookUser tmp : _friendList){
			if(tmp.getId().equals(id)){
				tmp.setColor(color);
			}			
		}
	}
	
	public String getNameFromId(String id) {
		// TODO Auto-generated method stub
		
		for(FacebookUser tmp : _friendList){
			if(tmp.getId().equals(id)){
				return tmp.getName();
			}			
		}
		return null;
	}
	
	public boolean idExist(String id){
		for(FacebookUser user: _friendList){
			if(user.getId().equals(id)){
				return true;
			}
		}		
		return false;
	}
	
	public boolean idHasJid(String id) {
		// TODO Auto-generated method stub
		for(FacebookUser user: _friendList){
			if(user.getId().equals(id)){
				if(user.getFjid() != null){
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}
	
	public void sortByUserSigne(String signe){
		ArrayList<FacebookUser> listSigne = new ArrayList<FacebookUser>();
		ListAstro astroList = new ListAstro();
		
		
		for(FacebookUser user: _friendList){
			if(signe.equals(user.getSigneAstro())){
				listSigne.add(user);
			} else {
				astroList.add(user);
			}
		}
		astroList.sortBySigne();
		
		_friendList.clear();
		_friendList.addAll(listSigne);
		_friendList.addAll(astroList.getList());
	}

	public void print() {
		Iterator<FacebookUser> it = _friendList.iterator();
		while (it.hasNext()) {
			FacebookUser tmp = it.next();
//			Log.d("Friend", "id: " + tmp.getId() + "  name: " + tmp.getName());
			Log.d("Friend", "id: " + tmp.getId());
			Log.d("Friend", "name: " + tmp.getName());
//			Log.d("friend", "first name: " + tmp.getFirstName());
//			Log.d("friend", "last name: " + tmp.getLastName());
			Log.d("Friend", "birthday: " + tmp.getBirth());
			Log.d("Friend", "signe: " + tmp.getSigneAstro());
			Log.d("Friend", "jid: " + tmp.getFjid());
			Log.d("Friend", "picture path: " + tmp.getPicturePath());
			Log.d("Friend", "time: " + tmp.getUpdatedTime());
			if(tmp.getPresence().isAvailable()){
				Log.d("Friend", "presence: " + tmp.getPresence().getType() + "/" + tmp.getPresence().getMode());
			} else {
				Log.d("Friend", "presence:" + tmp.getPresence().getType());
			}
			Log.d("Friend", " ");
		}
		Log.d("Friend", getSize() + " friends");
	}

	public void sortByPresence() {
		// TODO Auto-generated method stub
		ArrayList<FacebookUser> listAvailable = new ArrayList<FacebookUser>();
		ArrayList<FacebookUser> listUnavailable = new ArrayList<FacebookUser>();
		
		for(FacebookUser friend: getArrayList()){
			if(friend.getPresence().isAvailable()){
				listAvailable.add(friend);
			} else {
				listUnavailable.add(friend);
			}
		}
		
		_friendList.clear();
		_friendList.addAll(listAvailable);
		_friendList.addAll(listUnavailable);
	}
}
